<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$this->add_member();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
$return['basic']=$basic;
$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']));
if($_RQ['op']=='main'){
	$id=intval($_RQ['pid']);
	if($id){
		$post=pdo_get('jy_sort_item',array('id'=>$id));
		$post['images']=json_decode($post['images'],true);
		$post['lat_lng']=json_decode($post['lat_lng'],true);
		$typeid=$post['type'];
		$return['isid']=1;
		$post['pcd']=array('province'=>$post['province'],'city'=>$post['city'],'district'=>$post['district']);

	}else{
		$typeid=intval($_RQ['typeid']);
		$post=array('isshow'=>1,'realname'=>$member['nickname'],'age'=>18,'sex'=>'女','type_id'=>$_RQ['typeid']);
		$post['pcd']=$this->session('pcd');
		$post['lat_lng']=array('lat'=>'','lng'=>'');
	}
	$item_type=pdo_get('jy_sort_item_type',array('id'=>$typeid));
	set_html('title','发布'.$item_type['title']);
	if($item_type){
		if($id){
			$diyfileds=json_decode($post['diyfileds'],true);
		}else{
			$diyfileds = json_decode($item_type['diyfileds'],true);
		}
		$post['type_id'] = $item_type['id'];
		$post['diyfileds'] = $diyfileds;
	}
	/*
	$member=pdo_get('jy_sort_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	//判断是否是会员
	if($member['isvip']==1 && $member['viptime']>=time()){
		$member['ishongbao']=1;
	}else{
		$member['ishongbao']=0;
	}
	*/
	//是否关注
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$isfabunums = pdo_count('jy_sort_item',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	//	if($setting['maxfabu'] >0 && ($isfabunums >= $setting['maxfabu'])){
	//		exi('已达该活动的发布上限！','error');
	//	}
		if(!$postdata['title']){
			exi('标题不能为空！','error');
		}
		if(!$postdata['remark']){
			exi('说明不能为空！','error');
		}
		
		if(!$postdata['images']){
			//exi('图片不能为空！','error');
		}

		if(!$postdata['name']){
			exi('联系人姓名不能为空！','error');
		}

		if(!$postdata['sex']){
			//exi('请选择性别！','error');
		}
		if(empty($postdata['lat_lng'])){
			exi('请选择坐标！','error');
		}
		if(empty($postdata['mobile'])){
			exi('请填写联系人电话！','error');
		}
		//判断字段必填
		if(!empty($postdata['diyfileds'])){
			foreach($postdata['diyfileds'] as $item){
				if($item['is_must'] && empty($item['data'])){
					exi('请填写:'.$item['title'],'error');
				}
			}
		}
		if($postdata['pcd']){
			$address=$postdata['pcd']['province'].' '.$postdata['pcd']['city'];
			$code=pdo_getcolumn('core_area',array('address'=>$address),'parent_code');
		}
		$insert=array(
			'type'=>$postdata['type_id'],
			'title'=>$postdata['title'],
			'name'=>$postdata['name'],
			'code'=>$code,
			'province'=>$postdata['pcd']['province'],
			'city'=>$postdata['pcd']['city'],
			'district'=>$postdata['pcd']['district'],
			'address'=>$postdata['address'],
			'mobile'=>$postdata['mobile'],
			'weixin'=>$postdata['weixin'],
			'age'=>$postdata['age'],
			'sex'=>$postdata['sex'],
			'isbuy'=>$postdata['isbuy'],
			'classifyid'=>$postdata['classifyid'],
			'timesid'=>$postdata['timesid'],
			'price'=>$postdata['price'],
			'pay'=>$postdata['pay'],
			'company_title'=>$postdata['company_title'],
			'company_jobname'=>$postdata['company_jobname'],
			'lev'=>$postdata['lev'],
			'educationid'=>$postdata['educationid'],
			'school'=>$postdata['school'],
		//	'status'=>$status,
			'remark'=>$postdata['remark'],
			'images'=>ijson_encode($postdata['images']),
			'lat_lng'=>ijson_encode($postdata['lat_lng']),
			'diyfileds'=>ijson_encode($postdata['diyfileds']),
			'redmoney'=>$postdata['redmoney'],
			'rednum'=>$postdata['rednum'],
		);
		if($postdata['id']){
			pdo_update('jy_sort_item',$insert,array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
			exi('修改成功','success','jy_sort/myitem/main');
		}else{
			//判断是否需要付费
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$vip=pdo_get('jy_sort_vip',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
			$count=pdo_count('jy_sort_item',array('createtime'=>$daytime,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
			$insert['createtime']=TIMESTAMP;
			$insert['uniacid']=$_SESSION['uniacid'];
			$insert['mid']=$_SESSION['uid'];
			$msg='发布成功';
			if($basic['item_shenhe']){
				$insert['status']=0;
				$msg='发布成功,等待审核！';
			}else{
				$insert['status']=1;
			}
			if(($basic['ispay']==0 || ($basic['ispay']==1 && $vip['isvip']==1) || ($basic['ispay']==1 && $count<$basic['freetime'])) && ($basic['isred']!=1 || !$postdata['redmoney'])){
				pdo_insert('jy_sort_item',$insert);
				$id=pdo_insertid();
				exi($msg,'success','jy_sort/myitem/main');
			}else{
				if($basic['isred'] && $postdata['redmoney']){
					if($postdata['redmoney']<=0){
					   exi('红包金额需大于0元','error');
					}
					if(!$postdata['rednum'] || $postdata['rednum']<=0){
					   exi('红包数量需大于0','error');
					}
					$insert['fee']=$postdata['redmoney'];
					$peytitle='发布信息红包订单';
				}else{
					$peytitle='发布信息支付订单';
					$insert['fee']=$basic['fabufee'];
				}
				$insert['status']=-1;
				pdo_insert('jy_sort_item',$insert);
				$id=pdo_insertid();
				//支付
				$params=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'ordersn'=>get_ordersn('jy_sort_fabulog','ordersn'),
					'type'=>$postdata['type_id'],
					'title'=>$postdata['title'],
					'itemid'=>$id,
					'fee'=>$insert['fee'],
					'createtime'=>TIMESTAMP,
					'ispay'=>0
				);
				pdo_insert('jy_sort_fabulog',$params);
				$logid=pdo_insertid();
				$params['fee']=$insert['fee'];
				$params['title']=$peytitle;
				$params['id']=$logid;
				$params['plugin']='jy_sort';
				//$params['pay_type']='wechat';//可选，此项作用为指定支付方式
				//$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
				$params['back_url']='jy_sort/submit/payresult/';
				$return['payid']=cfc('pay')->create_pay_order($params);
				exi($return);
			}
		}
	}
	/*
	if((!$member['isvip'] || $member['viptime'] < time()) && $basic['isfee_hongbao']){
		$member['ishongbao'] =0;
	}else{
		$member['ishongbao'] =1;
	}
	*/
	$post['opus_images']=array();
	$post['mobile']=$post['mobile']?$post['mobile']:$member['mobile'];
	$post['name']=$post['name']?$post['name']:$member['nickname'];
	$return['post']=$post;
	$return['list']=$list;
	$return['item_type']=$item_type;
	$return['member']=$member;
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['educationid']=array('1'=>'初中及以下','2'=>'高中、中专','3'=>'大专','4'=>'本科','5'=>'研究生及以上');
	$return['timesid']=array('1'=>'一年以下','2'=>'一至三年','3'=>'三到五年','4'=>'五年以上');
	$return['classifyid']=array('1'=>'电脑办公','2'=>'家具家电','3'=>'手机','4'=>'文体户外','5'=>'服饰配饰','6'=>'儿童母婴','7'=>'美容保健','8'=>'数码产品','9'=>'居家日常','10'=>'其他');
	$return['isbuy']=array('0'=>'求购','1'=>'出售','3'=>'转让');
	$return['lev']=array('1'=>'轿车','2'=>'SUV','3'=>'MPV','4'=>'微面','5'=>'皮卡','6'=>'电动车','7'=>'其他');
	$return['pay']=array('0'=>'面议','1'=>'1-1000','2'=>'1000-2000','3'=>'2000-3000','4'=>'3000-5000','5'=>'5000-8000','6'=>'8000-10000','7'=>'1000-15000','8'=>'15000以上');
	$return['province']=cfc('tools')->pcd();
	$return['os']=$_SESSION['os'];
	exi($return);

}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_sort_fabulog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	if($basic['item_shenhe']){
		$status=0;
	}else{
		$status=1;
	}
	pdo_update('jy_sort_fabulog',array('status'=>1,'ispay'=>1,'fee'=>$payresult['fee'],'paytime'=>time()),array('id'=>$order['id']));
	pdo_update('jy_sort_item',array('status'=>$status,'ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['itemid']));
	exi('发布成功','success','jy_sort/myitem/main');
}
if($_RQ['op']=='getfileds'){
	//查询自定义字段
	$fileds=pdo_getcolumn('jy_sort_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'fileds');
	$fileds = json_decode($fileds,true);
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	$return['post']=array();
	$return['post']['fileds']=$fileds;
	$return['success']=1;
	exi($return);
}